<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<html:header title="待入库单详情">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">

        .layui-table-view{
            margin: 0;
        }
    </style>

    <script>
        //获取关联单结构体
        function genLinOrder(d) {
            let vo = {};
            if (d.type ==1) {
                vo = d.inGoodsVo;
                vo.linkUnit = d.inGoodsVo.provider;
                return vo;
            }
            if (d.type ==2) {
                vo = d.refundVo;
                vo.linkUnit = d.refundVo.custom;
                return vo;
            }

            if (d.type ==3) {
                vo = d.goodsTransferVo;
                vo.linkUnit = "";
                return vo;
            }
            if (d.type ==4) {
                vo = d.borrowInVo;
                vo.linkUnit = d.borrowInVo.provider;
                return vo;
            }
            if (d.type ==5) {
                vo = d.borrowOutBackVo;
                vo.linkUnit = d.borrowOutBackVo.custom;
                return vo;
            }
        }

        jQuery(function () {
            fixTableOperatorButton("${baseUrl}");
            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId");
            $("#cancel").click(function () {
                closeSubIframe();
            });

            $("#houseIn").click(function () {
                openTab("新增入库单","${baseUrl}house/houseInNew.action?houseWaitInId=${id}");
            });

            $("#linkNo").click(function () {
                openTab($(this).attr("detailTitle"),$(this).attr("detailUrl"));
            });

            layui.use(['form', 'table', 'laydate','element'], function () {
                let $ = layui.jquery,
                    form = layui.form,
                    table = layui.table,
                    element = layui.element,
                    laydate = layui.laydate,
                    util = layui.util;

                get("${baseUrl}house/houseIn/listVoByHouseWaitIn.action?houseWaitInId=${id}", function (res) {
                    let list=[];
                    $.each(res.data, function (i,houseInVo) {
                        $.each(houseInVo.houseInGoodsVoList, function (i,d) {
                            let vo = d.goodsId==null? d.packetVo:d.goodsVo;
                            vo.close = houseInVo.close;
                            vo.goodsId = d.goodsId;
                            vo.packetId = d.packetId;
                            vo.number = d.number;
                            vo.houseInNo = houseInVo.no;
                            vo.houseInId = houseInVo.id;
                            vo.unitId = d.unitId;
                            vo.remark = d.remark;
                            list.push(vo);
                        });


                        $.each(houseInVo.houseInPacketVoList, function (i,d) {
                            let vo = d;
                            vo.close = houseInVo.close;
                            vo.packetId = d.packetId;
                            vo.no = d.packetNo;
                            vo.name = d.packetName;
                            vo.number = d.number;
                            vo.houseInNo = houseInVo.no;
                            vo.houseInId = houseInVo.id;
                            vo.unitId = d.unitId;
                            vo.remark = d.remark;
                            list.push(vo);
                        });
                    });
                    renderTable2(list);
                });

                get("${baseUrl}house/houseWaitIn/getVoById.action?id=${id}", function (res) {
                    if (res.code == 1) {
                        $("#printAll").click(function () {
                            document.getElementById("printFrame").contentWindow.printAllPage(res.data);
                        });
                        $("#printWaitIn").click(function () {
                            document.getElementById("printFrame").contentWindow.printWaitIn(res.data);
                        });
                        $("#printHasIn").click(function () {
                            document.getElementById("printFrame").contentWindow.printHasIn(res.data);
                        });

                        if (res.data.type ==1) {
                            $("#typeName").html("进货");
                            $("#linkNo").html(res.data.inGoodsVo.no);
                            $("#linkNo").attr("detailUrl","${baseUrl}inGoods/inGoodsDetail.action?id="+res.data.inGoodsVo.id);
                            $("#linkNo").attr("detailtitle","进货单详情");
                        }
                        if (res.data.type ==2) {
                            $("#typeName").html("销售退货");
                            $("#linkNo").html(res.data.refundVo.no);
                            $("#linkNo").attr("detailUrl","${baseUrl}sale/refundDetail.action?id="+res.data.refundVo.id);
                            $("#linkNo").attr("detailtitle","进货单详情");
                        }
                        if (res.data.type ==3) {
                            $("#typeName").html("调拨");
                        }
                        if (res.data.type ==4) {
                            $("#typeName").html("借入");
                        }
                        if (res.data.type ==5) {
                            $("#typeName").html("借出归还");
                        }

                        let linkOrder = genLinOrder(res.data);

                        $("#user").html(linkOrder.user.name);
                        $("#date").html(GMTToDate(linkOrder.date));

                        if (linkOrder.linkUnit != "") {
                            $("#linkUnit").html(linkOrder.linkUnit.name);
                        } else {
                            $("#linkUnit").html("无");
                        }


                        $("#house").html(res.data.houseVo.store.name + ">" + res.data.houseVo.name);

                        let list1=[];
                        let list3=[];
                        $.each(res.data.houseWaitInGoodsVoList, function (i,d) {
                            let vo = d.goodsId==null? d.packetVo:d.goodsVo;
                            vo.goodsId = d.goodsId;
                            vo.packetId = d.packetId;
                            vo.waitInNumber = d.waitInNumber;
                            vo.hasInNumber = d.hasInNumber;
                            vo.number = d.number;
                            vo.unitId = d.unitId;
                            vo.remark = d.remark;
                            list1.push(vo);
                            if (d.waitInNumber > 0) {
                                list3.push(vo);
                            }
                        });

                        $.each(res.data.houseWaitInPacketVoList, function (i,d) {
                            let vo = d;
                            vo.packetId = d.packetId;
                            vo.no = d.packetNo;
                            vo.name = d.packetName;
                            vo.waitInNumber = d.waitInNumber;
                            vo.hasInNumber = d.hasInNumber;
                            vo.number = d.number;
                            vo.unitId = d.unitId;
                            vo.remark = d.remark;
                            list1.push(vo);
                            if (d.waitInNumber > 0) {
                                list3.push(vo);
                            }
                        });
                        renderTable1(list1);
                        renderTable3(list3);
                    }else {
                        tip("获取订单信息失败");
                    }
                });
            });
        });



        function renderTable1(list) {
            layui.table.render({
                id: 'table1',
                elem: '#table1',
                data: list,
                totalRow:true,
                text: {
                    none: '无' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {width:60,title:"序号",templet:function (d) {
                        return d.LAY_INDEX;
                    }},
                    {field: 'name', minWidth: 140, title: '名称/规格',style:"font-size:12px;",templet:"#nameBar"},
                    {field: 'no', width: 140, title: '商品编号', style: "font-size:12px;"},
                    {width: 120, title: '属性', style: "font-size:12px;", templet: function (d) {
                        return genAttr(d.attrNameVoList);
                    }},
                    {field:'number', width: 110, title: '数量'},
                    {field:'waitInNumber', width: 110, title: '未入库数量'},
                    {field:'hasInNumber', width: 110, title: '已入库数量'},
                    {width: 120, title: '单位', style: "font-size:12px;", templet: function (d) {
                        return d.goodsId==null ? "": genUnitName(d.goodsUnitVoList,d.unitId);
                    }},
                    {field: 'remark', width: 120, title: '备注', style: "font-size:12px;"},

                ]],
                limit: Number.MAX_VALUE,
                done: function (res, curr, count) {
                    $('[lay-id="table1"]').find(".packetName").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table1;
                        showPacketList(tableData[index].name, tableData[index].packetGoodsVoList);
                    });

                    let number = 0;
                    $.each(res.data, function (i,d) {
                        number += parseInt(d.number);
                    });
                    setTotalRowWithParent($('[lay-id="table1"]'),"number",number);
                    setTotalRowWithParent($('[lay-id="table1"]'),"0","合计");
                }
            });
        }

        function renderTable2(list) {
            layui.table.render({
                id: 'table2',
                elem: '#table2',
                data: list,
                totalRow: true,
                text: {
                    none: '无' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {width:60,title:"序号",templet:function (d) {
                            return genTableIndex(d.close, "作废", d);
                        }},
                    {field: 'name', minWidth: 140, title: '名称/规格', style: "font-size:12px;", templet: "#nameBar"},
                    {field: 'no', width: 140, title: '商品编号', style: "font-size:12px;"},
                    {
                        width: 120, title: '属性', style: "font-size:12px;", templet: function (d) {
                            return genAttr(d.attrNameVoList);
                        }
                    },
                    {field: 'number', width: 110, title: '数量'},
                    {field: 'houseInNo', width: 140, title: '入库单号',templet:'#houseInBar'},
                    {
                        width: 120, title: '单位', style: "font-size:12px;", templet: function (d) {
                            return d.goodsId == null ? "" : genUnitName(d.goodsUnitVoList, d.unitId);
                        }
                    },

                    {field: 'remark', width: 120, title: '备注', style: "font-size:12px;"},

                ]],
                limit: Number.MAX_VALUE,
                done: function (res, curr, count) {
                    $('[lay-id="table2"]').find(".packetName").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table2;
                        showPacketList(tableData[index].name, tableData[index].packetGoodsVoList);
                    });

                    $(".houseInDetail").unbind().bind('click',function () {
                        openTab("入库单详情", "${baseUrl}house/houseInDetail.action?id="+$(this).attr("houseInId"));
                    });

                    let number = 0;
                    $.each(res.data, function (i, d) {
                        if (!d.close){
                            number += parseInt(d.number);
                        }
                    });

                    setTotalRowWithParent($('[lay-id="table2"]'),"number",number);
                    setTotalRowWithParent($('[lay-id="table2"]'),"0","合计");

                    setTableLineCss("table2",res.data, "close", true, "font-gray-c2c2c2");
                }
            });
        }

        function renderTable3(list) {
            layui.table.render({
                id: 'table3',
                elem: '#table3',
                data: list,
                totalRow:true,
                text: {
                    none: '无' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {width:60,title:"序号",templet:function (d) {
                            return d.LAY_INDEX;
                        }},
                    {field: 'name', minWidth: 140, title: '名称/规格',style:"font-size:12px;",templet:"#nameBar"},
                    {field: 'no', width: 140, title: '商品编号', style: "font-size:12px;"},
                    {width: 120, title: '属性', style: "font-size:12px;", templet: function (d) {
                            return genAttr(d.attrNameVoList);
                        }},
                    {field:'waitInNumber', width: 110, title: '数量'},
                    {width: 120, title: '单位', style: "font-size:12px;", templet: function (d) {
                            return d.goodsId==null ? "": genUnitName(d.goodsUnitVoList,d.unitId);
                        }},
                    {field: 'remark', width: 120, title: '备注', style: "font-size:12px;"},

                ]],
                limit: Number.MAX_VALUE,
                done: function (res, curr, count) {
                    $('[lay-id="table3"]').find(".packetName").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table3;
                        showPacketList(tableData[index].name, tableData[index].packetGoodsVoList);
                    });

                    let number = 0;
                    $.each(res.data, function (i,d) {
                        number += parseInt(d.waitInNumber);
                    });
                    setTotalRowWithParent($('[lay-id="table3"]'),"waitInNumber",number);
                    setTotalRowWithParent($('[lay-id="table3"]'),"0","合计");
                }
            });
        }
    </script>

</html:header>

<iframe id="printFrame" style="width:0px;height:0px" src="${baseUrl}print/houseWaitInPrint.action" frameborder="0"></iframe>
<body>

<div class="layui-inline" style="margin-bottom: 20px;">
    <a class="layui-btn layui-btn-primary" id="cancel">关闭</a>
</div>
<div class="layuimini-container">
    <div class="layuimini-main">
        <input type="hidden" name="id" value="${id}">


        <h2 class="tableBeforeTitle">待入库单</h2>
        <div style="height: 4px;background-color: #bbbbbb;width: 100%;margin: 20px 0 2px 0;"></div>
        <div style="height: 1px;width: 100%;background-color: #dddddd;margin-bottom: 10px;"></div>
        <div style="height: 10px;"></div>


        <div class="layui-inline" style="margin:0 20px 10px 0;">
            <div>
                <span style="color: #aaaaaa">关联<span id="typeName"></span>单号:</span>
                <a href="#" id="linkNo" name="linkNo"></a>
            </div>
        </div>

        <div class="layui-inline" style="margin:0 20px 10px 0;">
            <div>
                <span style="color: #aaaaaa">往来单位：</span>
                <span id="linkUnit"></span>
            </div>
        </div>

        <div class="layui-inline" style="margin:0 20px 10px 0;">
            <div>
                <span style="color: #aaaaaa">入库仓库：</span>
                <span id="house"></span>
            </div>
        </div>

        <div class="layui-inline" style="margin:0 20px 10px 0;">
            <div>
                <span style="color: #aaaaaa">关联单经手人：</span>
                <span id="user"></span>
            </div>
        </div>

        <div class="layui-inline" style="margin:0 20px 10px 0;">
            <div>
                <span style="color: #aaaaaa">关联单日期：</span>
                <span id="date"></span>
            </div>
        </div>

        <div style="clear:both;height: 20px;"></div>

        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">全部商品</li>
                <li>已入库商品</li>
                <li>未入库商品</li>
            </ul>
            <div class="layui-tab-content" style="padding:0">
                <div class="layui-tab-item layui-show" style="padding: 20px 0;">
                    <table class="layui-hide" id="table1" lay-filter="table1" lay-size="sm"></table>
                </div>
                <div class="layui-tab-item" style="padding: 20px 0;">
                    <table class="layui-hide" id="table2" lay-filter="table2" lay-size="sm"></table>
                </div>
                <div class="layui-tab-item" style="padding: 20px 0;">
                    <table class="layui-hide" id="table3" lay-filter="table3" lay-size="sm"></table>
                </div>
            </div>
        </div>

        <div style="float: right; padding: 0 0 20px 20px;">
            <div class="layui-inline" style="padding-right: 10px;">
                <a href="javascript:;" class="layui-btn" id="houseIn">入库</a>
            </div>

            <html:drop-menu menu="打印全部商品" id="printAll">
                <a href="javascript:;" id="printHasIn">打印已入库商品</a>
                <a href="javascript:;" id="printWaitIn">打印未入库商品</a>
            </html:drop-menu>
        </div>

        <div style="clear: both"/>

    </div>
</div>

</body>


<script type="text/html" id="nameBar">
    {{#  if(d.goodsId==null){ }}
    <div class="packetName">
        {{d.name}}
    </div>
    {{#  }else{ }}
    {{genGoodsName(d.name,d.norms)}}
    {{#  } }}
</script>

<script type="text/html" id="houseInBar">
    <a href="#" class="houseInDetail" houseInId="{{d.houseInId}}">{{d.houseInNo}}</a>
</script>
<html:packetListDialog/>
<html:loading/>
</html>